package com.xiaoxian.org.controller;

import com.xiaoxian.basic.annotation.PreAuthorize;
import com.xiaoxian.basic.exception.CustomException;
import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.PageList;
import com.xiaoxian.org.domain.Department;
import com.xiaoxian.org.query.DepartmentQuery;
import com.xiaoxian.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")//打在Controller对Controller做描述
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * @GetMapping("/{id}") 查询一条  一般使用于查询
     * @PathVariable("获取指定那个参数")   从url中获取id  参数
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )//打在方法上，对方法做描述
    @GetMapping("/{id}")
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queryOne(id);
    }

    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }
    /**
     * 增删和修改
     * @PutMapping 一般用于增删和修改
     * @param department
     */
    @PutMapping
    public AjaxResult save(@RequestBody @Valid Department department){

            if (department.getId() == null) {
                departmentService.add(department);
                System.out.println(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.my();

    }

    /**
     * 通过id删除
     * @param id
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name="删除角色",value = "department:delete")
    public AjaxResult delete(@PathVariable Long id){

            departmentService.delete(id);
            return AjaxResult.my();

    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    public PageList<Department> queryList(@RequestBody DepartmentQuery departmentQuery){



        return departmentService.queryPage(departmentQuery);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){

            departmentService.patchDelete(ids);
            return AjaxResult.my();

    }


    @GetMapping("/tree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
